//
//  AppDelegate+Setup.m
//  Shuaicheyeguan
//
//  Created by xiangtao on 2025/3/26.
//

#import "AppDelegate+Setup.h"
#import "SCAppAppearance.h"
#import <IQKeyboardManager/IQKeyboardManager.h>
#import <React/RCTBridge.h>
#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>
#import "SCRNEventManager.h"
#import "SCNavigationController.h"
#import <OneLoginSDK/OneLoginSDK.h>
#import <MagicalRecord/MagicalRecord.h>
#import "SCHttpManager.h"
#import "Config.h"

static NSString *const kSCModelName = @"SCCoreData";
static NSString *const kSCDataStoreName = @"SCCoreData.sqlite";

@implementation AppDelegate (Setup)

- (void)setup {
  
  [self setupMagicalRecord];
  
  [[SCAppConfig sharedInstance] config];
  
  //统一设置appappearance
  [SCAppAppearance loadAppearance];
  
  //键盘设置
  [IQKeyboardManager sharedManager].enable = YES;
  
  //一键登录
//  [OneLogin setLogEnabled:YES];
  [OneLogin registerWithAppID:GTOneLoginAppId];
//  [OneLogin preGetTokenWithCompletion:^(NSDictionary * _Nonnull preResult) {
//    NSLog(@"preResult = %@",preResult);
//      if (preResult.count > 0 && preResult[@"status"] && 200 == [preResult[@"status"] integerValue]) {
//      } else {    // 预取号失败
//      }
//  }];
  
}

//注册企业微信
- (void)registWechatEnt {
//  bool isRegister = [WWKApi registerApp:@"wwauthac491099a7437b0c000004" corpId:@"wwac491099a7437b0c" agentId:@"1000004"];
//  NSLog(@"isRegister = %ld",isRegister);
//  return;
  
  [SCAppConfig sharedInstance].wxWorkRegister = NO;
  NSDictionary *param = @{kSource:@"app",kMerchantId:@([Config sharedConfig].user.merchantId)};
//  NSDictionary *param = @{kSource:@"app",kMerchantId:@(1000085)};
  [[SCHttpManager shareInstance] httpGet:@"userprod/merchantConfigure/getByMerchantId" param:param completion:^(id response, NSError *error) {
    if (response && [response isKindOfClass:NSDictionary.class]) {
        NSDictionary *appAttr = response[@"appAttr"];
        if (appAttr) {
          NSString *yeguanWeworkCorpId = appAttr[@"yeguanWeworkCorpId"];
          NSString *yeguanWeworkAgentId = appAttr[@"yeguanWeworkAgentId"];
          NSString *yeguanWeworkSchema = appAttr[@"yeguanWeworkSchema"];
          if (yeguanWeworkSchema && yeguanWeworkAgentId && yeguanWeworkCorpId) {
            [WWKApi registerApp:yeguanWeworkSchema corpId:yeguanWeworkCorpId agentId:yeguanWeworkAgentId];
            [SCAppConfig sharedInstance].wxWorkRegister = YES;
          }
        }
    }
  }];
}

- (void)setupMagicalRecord{
    [self copyDefaultStoreIfNecessary];
    [MagicalRecord setLoggingLevel:MagicalRecordLoggingLevelOff];
    [MagicalRecord setShouldAutoCreateManagedObjectModel:NO];
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:kSCModelName withExtension:@"momd"];
    NSManagedObjectModel *objectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
    [NSManagedObjectModel MR_setDefaultManagedObjectModel:objectModel];
    //自动版本迁移
    [MagicalRecord setupCoreDataStackWithAutoMigratingSqliteStoreNamed:kSCModelName];
}

// 将数据库文件拷贝到默认路径
- (void)copyDefaultStoreIfNecessary
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    NSURL *storeURL = [NSPersistentStore MR_urlForStoreName:kSCDataStoreName];
    
    // If the expected store doesn't exist, copy the default store.
    
    if (![fileManager fileExistsAtPath:[storeURL path]])
    {
        NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:[kSCDataStoreName stringByDeletingPathExtension] ofType:[kSCDataStoreName pathExtension]];
        
        if (defaultStorePath)
        {
            NSError *error;
            BOOL success = [fileManager copyItemAtPath:defaultStorePath toPath:[storeURL path] error:&error];
            if (!success)
            {
                NSLog(@"Failed to install default recipe store");
            }
        }
    }
}



@end
